<template>
  <div class="main">
    <mine-title>新建工程</mine-title>
    <div class="content" v-loading="loading">
      <el-row :gutter="20">
        <el-col :span="12">
          <div>
            <el-form
              ref="form"
              :model="form"
              label-width="110px"
              :rules="rules"
            >
              <el-form-item label="工程名称：" prop="name">
                <el-input v-model="form.name" size="small" clearable></el-input>
              </el-form-item>
              <el-form-item label="工程描述：" prop="explains">
                <el-input
                  type="textarea"
                  v-model="form.explains"
                  size="small"
                  clearable
                ></el-input>
              </el-form-item>
              <el-form-item label="所属项目：" prop="itemname">
                <el-input
                  v-model="form.itemname"
                  size="small"
                  clearable
                ></el-input>
              </el-form-item>
              <!-- <el-form-item label="创建者：" prop="creater">
                <el-input
                  v-model="form.creater"
                  size="small"
                  clearable
                ></el-input>
              </el-form-item> -->
              <el-form-item class="form-btn-row">
                <mine-button type="primary" @click="onCancel" secondary
                  >取 消
                </mine-button>
                <mine-button type="primary" @click="onSubmit"
                  >保 存</mine-button
                >
              </el-form-item>
            </el-form>
          </div>
        </el-col>
        <el-col :span="12"> </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import request from "@/modules/index/utils/request";
import { QWebChannel } from "@/modules/index/utils/qwebchannel.js";

export default {
  name: "AddProject",
  data() {
    return {
      form: {
        // creater: "",
        explains: "",
        iteminfoId: "",
        itemname: "",
        name: "",
        createTime: new Date(),
        id: "",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请填写工程名称",
            trigger: "blur",
          },
        ],
      },
      loading: false,
    };
  },
  computed: {},
  methods: {
    onSubmit() {
      let _this = this;
      this.$refs.form.validate(async (valid, object) => {
        let { form } = _this;
        if (valid) {
          // console.log(form);
          this.loading = true;
          let res = {};
          let _self = this;
          if (_self.$qwebUtil.isQtClient) {
            const uuid = _self.$qwebUtil.generateRandomUUID();
            let sendObject;
            /* eslint-disable-next-line */
            const channel = new QWebChannel(
              window.qt.webChannelTransport,
              async function (channel) {
                sendObject = channel.objects.sendObject;
                // receivedJSON 修改此处发送请求参数
                const receivedJSON = {
                  RequestUrl: "/feCanvas/save",
                  RequestMethod: "post",
                  ContentType: "application/json;charset=UTF-8",
                  RequestBody: form,
                  isStringParam: false,
                  requestId: uuid,
                };
                await send();
                await _self.$qwebUtil.receive(sendObject, receivedJSON);
                // console.error('received','/feCanvas/pageList')
              }
            );
            /* eslint-disable-next-line */
            async function send() {
              sendObject.sendMessage.connect(function (data) {
                if (!data) {
                  console.error("[Qt]", "No data received.");
                }
                res = JSON.parse(data);
                const result = res.response || {};
                if (result.success && res.webInfo.requestId == uuid) {
                  _self
                    .$confirm("保存成功，是否需要返回列表？", "提示", {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                    })
                    .then(() => {
                      _self.$router.replace("/featureEngineering");
                    })
                    .catch(() => {
                      // console.log("catch");
                      _self.loading = false;
                    });
                } else {
                  _self.loading = false;
                  _self.$message.error(result.msg || "新建失败");
                }
              });
            }
          } else if (this.$webview2.isWPF) {
            const uuid = this.$webview2.generateRandomUUID();
            await this.$webview2.sendWebView2(
              "/feCanvas/save",
              "post",
              "application/json;charset=UTF-8",
              form,
              false,
              uuid
            );
            await window.chrome.webview.addEventListener(
              "message",
              (e) => {
                let res = e.data;
                let result = res.response;
                // console.log(result);
                if (!result.success && res.response.uuid !== uuid) {
                  this.loading = false;
                  this.$message.error(result.msg || "新建失败");
                  return;
                } else {
                  _this
                    .$confirm("保存成功，是否需要返回列表？", "提示", {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                    })
                    .then(() => {
                      // console.log("success");
                      this.$router.replace("/featureEngineering");
                    })
                    .catch(() => {
                      // console.log("catch");
                      this.loading = false;
                    });
                }
              },
              { once: true }
            );
          } else {
            request({
              url: "feCanvas/save", //接口
              method: "post",
              data: form,
            }).then((response) => {
              let result = response.data;
              console.log(response);
              // console.log(result)
              if (!result.success) {
                this.loading = false;
                this.$message.error(result.msg || "新建失败");
                return;
              }
              this.$confirm("保存成功，是否需要返回列表？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
              })
                .then(() => {
                  this.$router.push("/featureEngineering");
                })
                .catch(() => {
                  this.loading = false;
                });
              // _this.onSubmitFile(result.data.id);
            });
          }
        } else {
          return false;
        }
      });
    },
    onCancel() {
      this.form = {
        itemName: "",
        detailed: "",
        unit: "",
        modelCount: "",
        remark: "",
        id: "",
      };
      this.$router.push("/featureEngineering");
    },
  },
  created() {
    this.$store.commit("SET_BREADCRUMB", [
      { title: "特征工程", path: "/featureEngineering" },
      { title: "新增工程", path: "" },
    ]);
  },
  beforeDestroyed() {},
};
</script>

<style scoped></style>
